//
//  XHSelectTypeViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHSelectTypeViewController.h"

#import "Services.h"

#define SCREEN_WIDTH                    ([UIScreen mainScreen].bounds.size.width)

@interface XHSelectTypeViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *ui_pickerView;
@property (weak, nonatomic) IBOutlet UIButton *ui_okButton;
@property (weak, nonatomic) IBOutlet UIButton *ui_cancel;

@property (strong ,nonatomic) NSArray * ui_array;
@property (strong, nonatomic) NSDictionary *typeDic;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_height;
@end

@implementation XHSelectTypeViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.la_height.constant = 0.5;
    self.ui_array = [[NSArray alloc]init];
    self.ui_array = @[@{@"title":@"全部",@"code":@""},
                      @{@"title":@"流量直充",@"code":kProductTypeFlow},
                      @{@"title":@"话费直充",@"code":kProductTypeCall},
                      @{@"title":@"智能缴费",@"code":kProductTypeIntelligent},
                      @{@"title":@"流量屯包",@"code":kProductTypeFlowPackage}
                      ];
    [self.ui_pickerView setShowsSelectionIndicator:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self updateView];
}

- (void)updateView{
    if (self.isOrderSelected) {
        self.ui_array=_ui_array= @[@{@"title":@"全部",@"code":@""},
                                   @{@"title":@"流量直充",@"code":kProductTypeFlow},
                                   @{@"title":@"话费直充",@"code":kProductTypeCall},
                                   @{@"title":@"智能缴费",@"code":kProductTypeIntelligent},
                                   @{@"title":@"流量屯包",@"code":kProductTypeFlowPackage},
                                   @{@"title":@"理财流量",@"code":kProductTypeFund}
                                   ];
        [self.ui_pickerView reloadAllComponents];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)cancelAction:(id)sender
{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}
- (IBAction)okAction:(id)sender
{
    if (self.typeDic)
    {
        [self.delegate didSelectedType:self.typeDic];
    }
    else
    {
        [self.delegate didSelectedType:self.ui_array.firstObject];
    }
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}

#pragma mark - UIPickerViewDelegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.typeDic = self.ui_array[row];
    UIView * view =  [self.ui_pickerView viewForRow:row forComponent:component];
    UILabel * label = view.subviews.firstObject;
    label.textColor =[ColorManager colorWithHexString:@"#5997A4"];
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSDictionary * dic = self.ui_array[row];
    return dic[@"title"];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (!view)
    {
        view = [[UIView alloc]init];
    }
    UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-35, 0, 70, 40)];
    NSInteger selecrDay  = [self.ui_pickerView selectedRowInComponent:component];
    if (selecrDay == row)
    {
        text.textColor = [ColorManager colorWithHexString:@"#000000"];
    }
    text.textAlignment = NSTextAlignmentCenter;
    text.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    [view addSubview:text];
    
    return view;
}


#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger rows = self.ui_array.count;
    return rows;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
